# -*- coding: utf-8 -*-

import scrapy
from test1.items import hc360cominfo
from scrapy import Request, FormRequest
from scrapyluke.uuCode import UUcode
from scrapy.loader import ItemLoader
from scrapyluke.processors import *
import datetime
import time


class CompanySpider(scrapy.Spider):

    name = 'heilongjiang'
    start_urls = ['http://gsxt.hljaic.gov.cn/']

    def __init__(self):
        super(CompanySpider, self).__init__()
        self.uu = UUcode('109521', '3c8906d3666e44bb9c961e1647126dbc')
        self.word = '哈哈科技'
        self.base_path = '/Users/bitfeng/spiders/imgs'

    # 请求验证码
    def parse(self, response):
        yield Request(url='http://gsxt.hljaic.gov.cn/validateCode.jspx?type=0', callback=self.code_ocr)

    # 验证码识别
    def code_ocr(self, response):
        img_abspath = self.uu.store_code(response.body, str(int(time.time()))+'.jpg', self.base_path)
        result = self.uu.check_code(img_abspath, 6001, 20)
        if result:
            code = result[1]
            # 判断验证码请求
            yield FormRequest(url='http://gsxt.hljaic.gov.cn/checkCheckNo.jspx', method='POST',
                              formdata={'checkNo': code}, meta={'code': code}, callback=self.check_code)

    # 验证验证码识别结果
    def check_code(self, response):
        if 'true' in response.body:
            data = {'checkNo': response.meta['code'], 'entName': self.word}
            # yield ...